package com.lantosec.hmrp.modules.workflow.controller;

import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.modules.workflow.service.IWorkflowDeployService;
import com.lantosec.hmrp.modules.workflow.vo.FlowModelVO;
import com.lantosec.hmrp.modules.workflow.service.IWorkflowRepositoryService;
import com.lantosec.hmrp.modules.workflow.vo.CreateModelVO;
import com.lantosec.hmrp.modules.workflow.vo.ModelVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.repository.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 系统数据流程定义表 控制器
 *
 * @author YCY
 * @since 2020-03-04
 */
@Api(value = "流程模型控制器", tags = "流程模型控制器")
@RestController
@RequestMapping("/workflow/model")
@Log4j2
public class WorkflowModelController {
    @Autowired
    private IWorkflowRepositoryService repositoryService;
    @Autowired
    private IWorkflowDeployService workflowDeployService;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<PageVO<FlowModelVO>> all(@ApiParam(value = "分页对象", required = true) PageVO page,
                                           @ApiParam(value = "模型名称") String modelName,
                                           @ApiParam(value = "模型类型ID") String modelTypeId) {
        try {
            return Result.ok(repositoryService.modelList(page, modelName, modelTypeId));
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("get/{modelId}")
    @ApiOperation(value = "查询模型")
    public Result<ModelVO> get(@ApiParam(value = "模型ID", required = true) @PathVariable(name = "modelId") String modelId) {
        try {
            ModelVO modelVO = repositoryService.queryModelById(modelId);
            return Result.ok(modelVO);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("getModelXml/{modelId}")
    @ApiOperation(value = "查询模型的XML文件")
    public Result<String> getModelXml(@ApiParam(value = "模型ID", required = true) @PathVariable(name = "modelId") String modelId) {
        try {
            String xml = repositoryService.modelEditorSource(modelId);
            // xml文件返回给前端编辑前替换自定义属性的名称，防止前端js解析失败
            xml = com.lantosec.hmrp.core.util.StringUtils.removeCustomAttrMark(xml);
            return Result.ok(xml);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("getModelPng/{modelId}")
    @ApiOperation(value = "查询模型的Png图片,图片以Base64形式返回")
    public Result<String> getModelPng(@ApiParam(value = "模型ID", required = true) @PathVariable(name = "modelId") String modelId) {
        try {
            return Result.ok(repositoryService.modelEditorSourceExtra(modelId));
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("create")
    @ControllerLog(name = "工作流创建模型", type = LogType.add)
    @ApiOperation(value = "创建模型，返回模型ID")
    public Result<String> createModel(@ApiParam(value = "模型分类", required = true)@RequestBody CreateModelVO createModelVO) {
        return repositoryService.createModel(createModelVO.getModelName(), createModelVO.getModelTypeId());
    }

    @PutMapping("save")
    @ControllerLog(name = "工作流保存流程模型数据", type = LogType.update)
    @ApiOperation(value = "保存流程模型数据")
    public Result<Void> save(@ApiParam(value = "模型名称", required = true) @RequestBody ModelVO model) {
        try {
            if(!Objects.isNull(model) && StringUtils.isNotEmpty(model.getDeploymentId())) {
                workflowDeployService.saveDeploymentInfo(model);
            }
            else {
                repositoryService.saveModel(model);
            }
            return Result.ok();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @DeleteMapping("del/{id}")
    @ControllerLog(name = "工作流删除流程模型", type = LogType.delete)
    @ApiOperation(value = "删除流程模型，慎用之")
    public Result<Void> delModel(@ApiParam(value = "模型ID", required = true) @PathVariable(name = "id") String modelId) {
        try {
            repositoryService.deleteModel(modelId);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
}
